package logic

import (
	"context"
	"strconv"

	"mall/common/modelx"
	"mall/service/backend/model"
	"mall/service/backend/rpc/internal/svc"
	"mall/service/backend/rpc/types/backend"

	"github.com/zeromicro/go-zero/core/logx"
)

type RecommendPostListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRecommendPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RecommendPostListLogic {
	return &RecommendPostListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RecommendPostListLogic) RecommendPostList(in *backend.RecommendPostListReq) (*backend.RecommendPostListResp, error) {
	if len(in.Date) == 6 {
		return l.getListByMonth(in)
	}

	return l.getListByDate(in)
}

func (l *RecommendPostListLogic) getListByMonth(in *backend.RecommendPostListReq) (*backend.RecommendPostListResp, error) {
	var recs []*backend.RecommendationInfo
	if i, err := strconv.ParseInt(in.Date, 10, 32); err == nil {
		filterMap := make(map[string]map[string][]interface{})
		filterMap[model.RecommendationColumns.StartDate] = map[string][]interface{}{
			"moreThan": []interface{}{i * 100},
		}
		filterMap[model.RecommendationColumns.EndDate] = map[string][]interface{}{
			"lessThan": []interface{}{i * 100 + 32},
		}
		filterMap[model.RecommendationColumns.Type] = map[string][]interface{}{
			"in": []interface{}{in.Type},
		}

		list, _, err := l.svcCtx.Recommendation.GetList(l.ctx, model.M{}, -1, filterMap)
		if err != nil {
			return nil, err
		}

		if len(list) > 0 {
			for _, item := range list {
				recs = append(recs, &backend.RecommendationInfo{
					Id:        int64(item.ID),
					EntryId:   int64(item.EntryID),
					ForumId:   int64(item.ForumID),
					TopicId:   int64(item.TopicID),
					PostId:    int64(item.PostID),
					Type:      item.Type,
					Status:    int32(item.Status),
					StartDate: int32(item.StartDate),
					CreatedAt: modelx.FormatTime(item.CreatedAt, ""),
					UpdatedAt: modelx.FormatTime(item.UpdatedAt, ""),
				})
			}
		}
	}

	return &backend.RecommendPostListResp{
		List: recs,
	}, nil
}

func (l *RecommendPostListLogic) getListByDate(in *backend.RecommendPostListReq) (*backend.RecommendPostListResp, error) {
	var recs []*backend.RecommendationInfo
	if i, err := strconv.ParseInt(in.Date, 10, 32); err == nil {
		filterMap := make(map[string]map[string][]interface{})
		filterMap[model.RecommendationColumns.StartDate] = map[string][]interface{}{
			"equalTo": []interface{}{i},
		}
		filterMap[model.RecommendationColumns.Type] = map[string][]interface{}{
			"in": []interface{}{in.Type},
		}

		list, _, err := l.svcCtx.Recommendation.GetList(l.ctx, model.M{}, -1, filterMap)
		if err != nil {
			return nil, err
		}

		if len(list) > 0 {
			for _, item := range list {
				recs = append(recs, &backend.RecommendationInfo{
					Id:        int64(item.ID),
					EntryId:   int64(item.EntryID),
					ForumId:   int64(item.ForumID),
					TopicId:   int64(item.TopicID),
					PostId:    int64(item.PostID),
					Type:      item.Type,
					Status:    int32(item.Status),
					StartDate: int32(item.StartDate),
					CreatedAt: modelx.FormatTime(item.CreatedAt, ""),
					UpdatedAt: modelx.FormatTime(item.UpdatedAt, ""),
				})
			}
		}
	}

	return &backend.RecommendPostListResp{
		List: recs,
	}, nil
}
